/**
 * Copyright 2013 CoderDream's Studio
 * All right reserved.
 * Created on 2013-4-5 ����12:49:07 by pig
 */
package com.pig.phttprequest;

import java.io.File;
import java.io.IOException;
import java.util.TreeMap;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.protocol.BasicHttpContext;

import com.pig.phttprequest.PHttpRequestCallBack.PHttpUploadFileCallback;
import com.pig.phttprequest.PMultiPartEntity.PostProgressListener;

/**
 * @author pig
 *
 */
public class PHttpRequestMultipartPost extends PHttpRequestPost {
	
	private PHttpUploadFileCallback httpUploadFileCallback = null;
	private MultipartEntity multipartContent;
	private long totalSize;
	
	public PHttpRequestMultipartPost(String url, TreeMap<String, String> params, MultipartEntity multipartEntity) {
		super(url, params);
		this.multipartContent = multipartEntity;
	}
	
	/**
	 * @param url
	 * @param params
	 */
	public PHttpRequestMultipartPost(String url, TreeMap<String, String> params) {
		super(url, params);
		// TODO Auto-generated constructor stub
	}
	
	public void setUploadFilePath(String filepath, String key) {
		if (null != filepath) {
			setUploadFile(new File(filepath), key);
		}
	}
	
	public void setUploadFile(File file, String key) {
		// multipart
		multipartContent = new PMultiPartEntity(new PostProgressListener() {

			@Override
			public void transferred(long num) {
				// TODO Auto-generated method stub
				if(null != httpUploadFileCallback) {
					httpUploadFileCallback.requestSendBytes(totalSize, num);
					httpUploadFileCallback.reqeustProgress(num / (float)totalSize);
				}
			}
			
		});
		
		multipartContent.addPart(key, new FileBody(file));
		totalSize = multipartContent.getContentLength();
	}
	
	/* (non-Javadoc)
	 * @see com.pig.phttprequest.PHttpRequestPost#executeRequestMethod()
	 */
	@Override
	protected HttpResponse executeRequestMethod() {
		// TODO Auto-generated method stub

		HttpResponse httpResponse = null;
		
		try {
			HttpPost postMethod = this.initRequestMethod();
			if(null == postMethod) {
				return httpResponse;
			}
			
			//file
			if(null != multipartContent){
				postMethod.setEntity(multipartContent);
			}
			
			httpResponse = super.requestClient.execute(postMethod, new BasicHttpContext());
			return httpResponse;
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

	/**
	 * @return the multipartContent
	 */
	public MultipartEntity getMultipartContent() {
		return multipartContent;
	}

	/**
	 * @param multipartContent the multipartContent to set
	 */
	public void setMultipartContent(MultipartEntity multipartContent) {
		this.multipartContent = multipartContent;
	}

	/**
	 * @param httpUploadFileCallback the httpUploadFileCallback to set
	 */
	public void setHttpUploadFileCallback(
			PHttpUploadFileCallback httpUploadFileCallback) {
		this.httpUploadFileCallback = httpUploadFileCallback;
	}

}
